//
//  AddExpViewController.m
//  jiyujiaProgram
//
//  Created by Jame on 15/12/15.
//  Copyright © 2015年 中良 赵. All rights reserved.
//

#import "AddExpViewController.h"

@interface AddExpViewController () <CDPDatePickerDelegate,UITextViewDelegate,UIAlertViewDelegate>

@end

@implementation AddExpViewController
{
    UILabel *_startLabel;
    UILabel *_endLabel;
    CDPDatePicker *_datePicker;
    BOOL _isStart;
    UIView *firstView;
    UITextView *_resonTextView;
    UILabel *_placeHoldLabel;

}


- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    [self creatNavigationBarWithImage:nil title:@"添加教学经历"];
    [self creatNavigationBarLeftItemWithLeftTitle:nil LeftImage:ECIMAGENAME(@"back_button.png")];
    [self creatNavigationBarRightItemWithRightTitle:@"提交" RightImage:nil];
    
    [self creatView];
    [self creatDatePicker];

}

- (void)leftBtnClick:(id)leftSender
{
    [self.navigationController popViewControllerAnimated:YES];
}

- (void)rightBtnClick:(id)rightSender
{
    if ([_startLabel.text isEqualToString:@"请选择日期"]) {
        SHOW_ALERT(@"提示", @"请选择起始时间", @"好的", nil, nil);
    }else if ([_endLabel.text isEqualToString:@"请选择日期"]){
        SHOW_ALERT(@"提示", @"请选择结束时间", @"好的", nil, nil);
    }else if (_resonTextView.text.length == 0){
        SHOW_ALERT(@"提示", @"请填写教学经历", @"好的", nil, nil);
    }else{
        
        NSString *start = [_startLabel.text stringByReplacingOccurrencesOfString:@"/" withString:@"-"];
        NSString *end = [_endLabel.text stringByReplacingOccurrencesOfString:@"/" withString:@"-"];
        
        NSDictionary *data = @{
                               @"teacher_Id":self.memberId,
                               @"startTime":start,
                               @"endTime":end,
                               @"descr":_resonTextView.text,
                               };
        
        NSDictionary *param = @{
                                @"data":data,
                                };
        
        [[HttpManager defaultManager] postRequestToUrl:RequestURL(UPDATEEXP) params:param complete:^(BOOL successed, NSDictionary *result) {
            if (successed) {
                if ([result[@"errCode"] integerValue] == 200) {
                    SHOW_ALERT(@"提示", result[@"errMsg"], @"好的", nil, self);
                }else{
                    SHOW_ALERT(@"提示", result[@"errMsg"], @"好的", nil, nil);
                }
            }
        }];
    }
}


- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex
{
    if (buttonIndex == 0) {
        [self.navigationController popViewControllerAnimated:YES];
    }
}

- (void)creatView
{
    firstView = [[UIView alloc] initWithFrame:CGRectMake(0, 64, ECSCREEN_W, ECSCREEN_H - 64)];
    firstView.backgroundColor = ECCOLOR(255, 255, 255, 1);
    [self.view addSubview:firstView];
    
    //startView
    UIView *startView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, ECSCREEN_W, 44)];
    startView.userInteractionEnabled = YES;
    [firstView addSubview:startView];
    
    UILabel *startTitle = [[UILabel alloc] initWithFrame:CGRectMake(10, 0, 80, 43)];
    startTitle.text = @"起始日期";
    startTitle.font = [UIFont systemFontOfSize:12.f];
    [startView addSubview:startTitle];
    
    _startLabel = [[UILabel alloc] initWithFrame:CGRectMake(90, 0, ECSCREEN_W - 70 - 20, 43)];
    _startLabel.text = @"请选择日期";
    _startLabel.textColor = [UIColor lightGrayColor];
    _startLabel.font = [UIFont systemFontOfSize:12.f];
    [startView addSubview:_startLabel];
    
    UIButton *startButton = [UIButton buttonWithType:UIButtonTypeCustom];
    startButton.tag = 300;
    [startButton addTarget:self action:@selector(dateClick:) forControlEvents:UIControlEventTouchUpInside];
    startButton.frame = CGRectMake(90, 0, ECSCREEN_W - 70 -20, 43);
    [startView addSubview:startButton];
    
    
//    UIImageView *startImage = [[UIImageView alloc] initWithFrame:CGRectMake(ECSCREEN_W - 40, 12, 20, 20)];
//    startImage.image = ECIMAGENAME(@"start.png");
//    startImage.clipsToBounds = YES;
//    startImage.layer.cornerRadius = 10;
//    [startView addSubview:startImage];
    
    UIView *startSperaLine = [[UIView alloc] initWithFrame:CGRectMake(0, 43, ECSCREEN_W, 1)];
    startSperaLine.backgroundColor = ECCOLOR(0, 0, 0, .3);
    [startView addSubview:startSperaLine];
    
    
    //endView
    UIView *endView = [[UIView alloc] initWithFrame:CGRectMake(0, 44, ECSCREEN_W, 44)];
    endView.userInteractionEnabled = YES;
    [firstView addSubview:endView];
    
    UILabel *endTitle = [[UILabel alloc] initWithFrame:CGRectMake(10, 0, 80, 43)];
    endTitle.text = @"结束日期";
    endTitle.font = [UIFont systemFontOfSize:12.f];
    [endView addSubview:endTitle];
    
    _endLabel = [[UILabel alloc] initWithFrame:CGRectMake(90, 0, ECSCREEN_W - 70 - 20, 43)];
    _endLabel.text = @"请选择日期";
    _endLabel.textColor = [UIColor lightGrayColor];
    _endLabel.font = [UIFont systemFontOfSize:12.f];
    [endView addSubview:_endLabel];
    
    
    UIButton *endButton = [UIButton buttonWithType:UIButtonTypeCustom];
    endButton.tag = 301;
    [endButton addTarget:self action:@selector(dateClick:) forControlEvents:UIControlEventTouchUpInside];
    endButton.frame = CGRectMake(90, 0, ECSCREEN_W - 70 -20, 43);
    [endView addSubview:endButton];
    
    
//    UIImageView *endImage = [[UIImageView alloc] initWithFrame:CGRectMake(ECSCREEN_W - 40, 12, 20, 20)];
//    endImage.image = ECIMAGENAME(@"stop.png");
//    endImage.clipsToBounds = YES;
//    endImage.layer.cornerRadius = 10;
//    [endView addSubview:endImage];
    
    UIView *endSperaLine = [[UIView alloc] initWithFrame:CGRectMake(0, 43, ECSCREEN_W, 1)];
    endSperaLine.backgroundColor = ECCOLOR(0, 0, 0, .3);
    [endView addSubview:endSperaLine];

    
    
    UIView *resonView = [[UIView alloc] initWithFrame:CGRectMake(0, 88, ECSCREEN_W, 100)];
    [firstView addSubview:resonView];
    
    UILabel *resonTitle = [[UILabel alloc] initWithFrame:CGRectMake(10, 0, 80, 44)];
    resonTitle.text = @"教学经历";
    resonTitle.font = [UIFont systemFontOfSize:12.f];
    [resonView addSubview:resonTitle];
    
    
    _resonTextView = [[UITextView alloc] initWithFrame:CGRectMake(85, 5, ECSCREEN_W - 90 - 20, 90)];
    _resonTextView.delegate = self;
    _resonTextView.font = [UIFont systemFontOfSize:12.f];
    [resonView addSubview:_resonTextView];
    
    
    _placeHoldLabel = [[UILabel alloc] initWithFrame:CGRectMake(5, 0, 100, 30)];
    _placeHoldLabel.text = @"请输入教学经历...";
    _placeHoldLabel.textColor = [UIColor lightGrayColor];
    _placeHoldLabel.font = [UIFont systemFontOfSize:12.f];
    [_resonTextView addSubview:_placeHoldLabel];

    
    UIView *resonSperaLine = [[UIView alloc] initWithFrame:CGRectMake(0, 99, ECSCREEN_W, 1)];
    resonSperaLine.backgroundColor = ECCOLOR(0, 0, 0, .3);
    [resonView addSubview:resonSperaLine];
}


- (void)dateClick:(UIButton *)button
{
    [_resonTextView resignFirstResponder];
    
    if (button.tag == 300) {
        _isStart = YES;
        [_datePicker pushDatePicker];
    }else{
            _isStart = NO;
            [_datePicker pushDatePicker];
    }
}

#pragma mark - creatDatePicker
- (void)creatDatePicker
{
    _datePicker=[[CDPDatePicker alloc] initWithView:self.view delegate:self];
    _datePicker.theTypeOfDatePicker = 2;
}


- (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event
{
    [_resonTextView resignFirstResponder];
    [_datePicker popDatePicker];
}

#pragma mark - CDPDatePickerDidConfirm
//回调，字符串可自行进行截取
-(void)CDPDatePickerDidConfirm:(NSString *)confirmString{
    if (_isStart) {
        _startLabel.textColor = [UIColor blackColor];
        _startLabel.text = confirmString;
    }else{
        _endLabel.textColor = [UIColor blackColor];
        _endLabel.text = confirmString;
    }
}


- (BOOL)textView:(UITextView *)textView shouldChangeTextInRange:(NSRange)range replacementText:(NSString *)text
{
    if (range.location>=140)
    {
        return  NO;
    }
    else
    {
        return YES;
    }
}

- (void)textViewDidChange:(UITextView *)textView
{
    if (_resonTextView.text.length == 0) {
        _placeHoldLabel.text = @"请输入教学经历...";
    }else{
        _placeHoldLabel.text = @"";
    }
}


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
